#include "app_device.h"

struct app_device
{
    bool is_running;
    buffer_t *down_buffer;
    buffer_t *up_buffer;
};

app_device_t *dev;

void mqtt_msg_cb(char *data, int len);
void modbus_rw_task(void *arg);

void app_upload_task(void *arg);
app_device_t *app_device_init(void)
{

    if (dev)
        return dev;

    dev = (app_device_t *)malloc(sizeof(app_device_t));

    // 1. 初始化缓冲区
    dev->down_buffer = app_buffer_init(1024);
    dev->up_buffer = app_buffer_init(1024);

    // 2. 初始化mqtt
    gate_status_t status = app_mqtt_init(mqtt_msg_cb);
    if (status != GATE_OK)
    {
        log_error("mqtt init error");
        app_buffer_deinit(dev->down_buffer);
        app_buffer_deinit(dev->up_buffer);
        free(dev);
        return NULL;
    }
    // 3. 初始化线程池
    status = app_pool_init(10);
    if (status != GATE_OK)
    {
        log_error("pool init error");
        app_buffer_deinit(dev->down_buffer);
        app_buffer_deinit(dev->up_buffer);
        app_mqtt_deinit();
        free(dev);
        return NULL;
    }
    // 4. modbus模块初始化  TODO
    app_modbus_init();

    // 5. 添加2个线程: modbus读写线程和数据上传线程
    log_info("启动任务....  modbus_rw_task");
    app_pool_add_task(modbus_rw_task, NULL);
    log_info("启动任务....  app_upload_task");
    app_pool_add_task(app_upload_task, NULL);
    log_info("启动任务....  完成");

    dev->is_running = true;
}

void app_device_deinit(app_device_t *dev)
{
    app_buffer_deinit(dev->down_buffer);
    app_buffer_deinit(dev->up_buffer);
    app_mqtt_deinit();
    app_pool_deinit();
    // modubs的反初始化
    free(dev);
}

void mqtt_msg_cb(char *data, int len)
{
    if (data == NULL || len <= 0)
    {
        log_error("mqtt_msg_cb: data is null");
        return;
    }
    log_info("mqtt收到的数据: %.*s", len, data);

    app_buffer_write(dev->down_buffer, data, len);
}
void app_upload_task(void *arg)
{
    char data[256];
    while (1)
    {
        int real_len = app_buffer_read(dev->up_buffer, data, sizeof(data));
        if (real_len > 0)
        {
            data[real_len] = '\0';
            app_mqtt_send(data, real_len);
        }
    }
}

void modbus_rw_task(void *arg)
{
    char data[256];
    while (1)
    {
        int real_len = app_buffer_read(dev->down_buffer, data, sizeof(data));
        if (real_len <= 0)
            continue;

        // json解析
        msg_t msg;
        app_msg_json_2_msg(data, &msg);
        // 打印msg结构体内容
        log_info("action: %s, connType: %s, motorId: %d, motorSpeed: %d, status: %s",
                 msg.action,
                 msg.connType,
                 msg.motorId,
                 msg.motorSpeed,
                 msg.status);
        if (strcmp(msg.action, "set") == 0)
        {
            // 调用modbus模块的写保持寄存器的函数  TODO
            // log_info("modbus_write_register");
            app_modbus_writeSingleHoldRegister(msg.motorId, (uint16_t)msg.motorSpeed);
        }
        else if (strcmp(msg.action, "get") == 0)
        {
            // 调用modbus模块的读输入寄存器的函数  TODO
            log_info("modbus_read_register");
            // 模拟读取到数据, 然后写入到上行缓冲区
            int res = app_modbus_readSigleInputRegister(msg.motorId, &msg.motorSpeed);
            if (res == GATE_OK)
            {
                msg.status = "ok";
            }
            else
            {
                msg.status = "error";
            }

            char json[256];
            memset(json, 0, sizeof(json));
            app_msg_msg_2_json(&msg, json);
            app_buffer_write(dev->up_buffer, json, strlen(json));
        }
    }
}

bool app_device_get_is_ruuning(void)
{
    return dev->is_running;
}

void app_device_set_is_ruuning(bool is_running)
{
    dev->is_running = is_running;
}
